#include "IniManager.h"
#include <QtCore/QtCore>

//IniManager::~IniManager() {
//	if (iniSettings != nullptr) {
//		delete iniSettings;
//		iniSettings = nullptr;
//	}
//}

void IniManager::iniRepair() {

}

void IniManager::loadIni(QString iniPath) {
	if (iniPath.isEmpty())
	{
		iniPath = QCoreApplication::applicationDirPath() + "/Config.ini";
	}
	std::string str = iniPath.toStdString();
	iniSettings = new QSettings(iniPath, QSettings::IniFormat);
	iniSettings->setIniCodec(QTextCodec::codecForName("GBK"));
	//tube
	xrayTubeConf.tube_com_no = getTubePort();
	xrayTubeConf.tube_name = getTubeType();

	//dt
	xrayCameraConf.scanner_name = getScannerType();
	xrayCameraConf.scanner_control_port = getScannerControlPort();
	xrayCameraConf.scanner_image_port = getScannerImgPort();
	xrayCameraConf.scanner_ip = getScannerIp();
	xrayCameraConf.scanner_daqip = getDaqIp();
	xrayCameraConf.scanner_int_time = getScannerIntegralTime();

	//belt 
	beltConf.belt_com_no = getBeltPort();
	beltConf.belt_speed = getBeltSpeed();

	//plc
	/*plcConf.plc_com_no = getPlcPort();*/

	//io
	ioPort = getIoPort();

	//system
	sysConf.language = getSysLang();
	sysConf.img_path = getImgPath();
	sysConf.log_path = getLogPath();
	sysConf.isSaveImg = getIsSaveImg();
	sysConf.collectionMode = getCollectionMode();
	sysConf.lineNum = getLineNum();

	//mysql
	sqlConf.dbType = getDBType();
	sqlConf.dbName = getDBName();
	sqlConf.charSet = getDBCharSet();
	sqlConf.host = getSqlIp();
	sqlConf.passwd = getSqlPasswd();
	sqlConf.port = getDBPort();
	sqlConf.userName = getSqlUserName();

	//sqlite
	sqliteConf.dbType = getDBType();
	sqliteConf.dbName = getSqliteDBName();

	forceCalibrate = false;
	isPlcEnabled = true;
}

void IniManager::destory() {
	if (iniSettings != nullptr) {
		delete iniSettings;
		iniSettings = nullptr;
	}
}

std::string IniManager::getDBType() {
	return iniSettings->value("db/dbtype").toString().toStdString();
}

void IniManager::setDBType(std::string dbType_) {
	iniSettings->setValue("db/dbtype", dbType_.c_str());
}

std::string IniManager::getSqlUserName() {
	return iniSettings->value("mysql/userName").toString().toStdString();
}

void IniManager::setMySqlUserName(std::string mysqlUserName_) {
	iniSettings->setValue("mysql/userName", mysqlUserName_.c_str());
}

std::string IniManager::getSqlPasswd() {
	return iniSettings->value("mysql/passwd").toString().toStdString();
}

void IniManager::setMySqlPasswd(std::string mysqlPwd_) {
	iniSettings->setValue("mysql/passwd", mysqlPwd_.c_str());

}

std::string IniManager::getSqlIp() {
	return iniSettings->value("mysql/host").toString().toStdString();
}

void IniManager::setMySqlIp(std::string ip) {
	iniSettings->setValue("mysql/host", ip.c_str());
}

std::string IniManager::getDBName() {
	return iniSettings->value("mysql/dbName").toString().toStdString();
}

void IniManager::setDBName(std::string dbName_) {
	iniSettings->setValue("mysql/dbName", dbName_.c_str());
}

std::string IniManager::getDBCharSet() {
	return iniSettings->value("mysql/charSet").toString().toStdString();
}
void IniManager::setDBCharSet(std::string dbDBCharSet_) {
	iniSettings->setValue("mysql/charSet", dbDBCharSet_.c_str());
}

int IniManager::getDBPort() {
	return iniSettings->value("mysql/port").toInt();
}

void IniManager::setDBPort(int port) {
	iniSettings->setValue("mysql/port", port);
}

std::string IniManager::getSqliteDBName() {
	return iniSettings->value("sqlite/dbName").toString().toStdString();
}

void IniManager::setSqliteDBName(std::string dbName_) {
	iniSettings->setValue("sqlite/dbName", dbName_.c_str());
}

int IniManager::getPollingTime() {
	int pollTime = 10;
	pollTime = iniSettings->value("disk/pollingTime").toInt();
	return pollTime;
}

void IniManager::setPollingTime(int pollingTime) {
	iniSettings->setValue("disk/pollingTime", pollingTime);
}

int IniManager::getSavFilDay() {
	int savFilDay = 7;
	savFilDay = iniSettings->value("disk/savFilDay").toInt();
	return savFilDay;
}

void IniManager::setSavFilDay(int savFilDay) {
	iniSettings->setValue("disk/savFilDay", savFilDay);
}

int IniManager::getAlarmRemSpace() {
	int remSpace = 10;
	remSpace = iniSettings->value("disk/alarmRemSpace").toInt();
	return remSpace;
}

void IniManager::setAlarmRemSpace(int remSpace) {
	iniSettings->setValue("disk/alarmRemSpace", remSpace);
}

std::string IniManager::getSysLang() {
	return iniSettings->value("system/language").toString().toStdString();
}
void IniManager::setSysLang(std::string sysLang_) {
	iniSettings->setValue("system/language", sysLang_.c_str());
}

std::string IniManager::getImgPath() {
	return iniSettings->value("system/img_path").toString().toStdString();
}
void IniManager::setImgPath(std::string imgPath_) {
	iniSettings->setValue("system/img_path", imgPath_.c_str());
}

std::string IniManager::getLogPath() {
	return iniSettings->value("system/log_path").toString().toStdString();
}
void IniManager::setLogPath(std::string logPath_) {
	iniSettings->setValue("system/log_path", logPath_.c_str());
}

bool IniManager::getIsCollectionMode() {
	if (iniSettings->value("system/isCollectionMode").toInt() == 1)
		return true;
	else {
		return false;
	}
}
void IniManager::setIsCollectionMode(bool status) {
	if (status) {
		iniSettings->setValue("system/isCollectionMode", 1);
	}
	else {
		iniSettings->setValue("system/isCollectionMode", 0);
	}	
}

COLLECTYPE IniManager::getCollectionMode() {
	if (iniSettings->value("system/CollectionMode").toInt() == 0)
		return COLLECTYPE::ALGO;
	else if (iniSettings->value("system/CollectionMode").toInt() == 1)
		return COLLECTYPE::CHECKOUTI;
	else
		return COLLECTYPE::CHECKOUTII;
}

void IniManager::setCollectionMode(COLLECTYPE collectionMode) {
	if(collectionMode == COLLECTYPE::ALGO)
		iniSettings->setValue("system/CollectionMode", 0);
	else if (collectionMode == COLLECTYPE::CHECKOUTI)
		iniSettings->setValue("system/CollectionMode", 1);
	else if (collectionMode == COLLECTYPE::CHECKOUTII)
		iniSettings->setValue("system/CollectionMode", 2);
	sysConf.collectionMode = collectionMode;
}

int IniManager::getLineNum() {
	return iniSettings->value("system/lineNum").toInt();
}

void IniManager::setLineNum(int num) {
	iniSettings->setValue("system/lineNum", num);
}

int IniManager::getScanPeriod() {
	return iniSettings->value("system/scanPeriod").toInt();
}

bool IniManager::getIsSaveImg() {

	if (iniSettings->value("system/isSaveImg").toInt() == 1)
		return true;
	else {
		return false;
	}
}
void IniManager::setIsSaveImg(bool status) {
	if (status) {
		iniSettings->setValue("system/isSaveImg", 1);
	}
	else {
		iniSettings->setValue("system/isSaveImg", 0);
	}
	sysConf.isSaveImg = status;
}


std::string IniManager::getTubePort() {
	return iniSettings->value("tube/port").toString().toStdString();
}
void IniManager::setTubePort(std::string port) {
	iniSettings->setValue("tube/port", port.c_str());
}

void IniManager::setTubeType(std::string tupeType) {
	iniSettings->setValue("tube/tupeType", tupeType.c_str());
}
std::string IniManager::getTubeType() {
	return iniSettings->value("tube/tupeType").toString().toStdString();
}

void IniManager::setScannerType(std::string scannerType) {
	iniSettings->setValue("scanner/tupeType", scannerType.c_str());
}

std::string IniManager::getScannerType() {
	return iniSettings->value("scanner/tupeType").toString().toStdString();
}

std::string IniManager::getScannerIp() {
	return iniSettings->value("scanner/hostIp").toString().toStdString();
}
void IniManager::setScannerIp(std::string ip) {
	iniSettings->setValue("scanner/hostIp", ip.c_str());
}

std::string IniManager::getDaqIp() {
	return iniSettings->value("scanner/daqIp").toString().toStdString();
}
void IniManager::setDaqIp(std::string ip) {
	iniSettings->setValue("scanner/daqIp", ip.c_str());
}

int IniManager::getScannerControlPort() {
	return iniSettings->value("scanner/controlPort").toInt();;
}

void IniManager::setScannerControlPort(int port) {
	iniSettings->setValue("scanner/controlPort", port);
}

int IniManager::getScannerImgPort() {
	return iniSettings->value("scanner/imgPort").toInt();;
}
void IniManager::setScannerImgPort(int port) {
	iniSettings->setValue("scanner/imgPort", port);
}

int IniManager::getScannerIntegralTime() {
	return iniSettings->value("scanner/intTime").toInt();;
}
void IniManager::setScannerIntegralTime(int time) {
	iniSettings->setValue("scanner/intTime", time);
}

std::string IniManager::getBeltPort() {
	return iniSettings->value("belt/port").toString().toStdString();
}
void IniManager::setBeltPort(std::string port) {
	iniSettings->setValue("belt/port", port.c_str());
}

float IniManager::getBeltSpeed() {
	return iniSettings->value("belt/speed").toFloat();
}

void IniManager::setBeltSpeed(float speed) {
	iniSettings->setValue("belt/speed", QString("%1").arg(speed));
}

bool IniManager::getBeltEnabledStatus() {
	if (iniSettings->value("belt/isBeltEnabled").toString() == "false") {
		return false;
	}
	return true;
}

bool IniManager::getIOEnabledStatus() {
	if (iniSettings->value("io/isIoEnabled").toString() == "false") {
		 return false;
	}
	return true;
}

int IniManager::getBuzzerConOutPort() {
	return iniSettings->value("buzzer/outPort").toInt();
}
void IniManager::setBuzzerConOutPort(int port) {
	iniSettings->setValue("buzzer/outPort", port);
}

int IniManager::getAirConOutPort() {
	return iniSettings->value("air/outPort").toInt();
}
void IniManager::setAirConOutPort(int port) {
	iniSettings->setValue("air/outPort", port);
}

bool IniManager::getHasFeedAgency() {
	if (iniSettings->value("feed/hasFeddAgency").toString() == "false") {
		return false;
	}
	return true;
}
void IniManager::setHasFeedAgency(bool hasFeed) {
	iniSettings->setValue("feed/hasFeddAgency", hasFeed);
}

int IniManager::getFeedConOutPort() {
	return iniSettings->value("feed/outPort").toInt();
}
void IniManager::setFeedConOutPort(int port) {
	iniSettings->setValue("feed/outPort", port);
}

int IniManager::getRejectConOutPort() {
	return iniSettings->value("reject/outPort").toInt();
}
void IniManager::setRejectConOutPort(int port) {
	iniSettings->setValue("reject/outPort", port);
}
int IniManager::getRejectType() {
	return iniSettings->value("reject/rejectType").toInt();
}
void IniManager::setRejectType(int type) {
	iniSettings->setValue("reject/rejectType", type);
}
bool IniManager::getRejectConsecutive() {
	if (iniSettings->value("reject/isRejectConsecutive").toString() == "false") {
		return false;
	}
	return true;
}
void IniManager::setRejectConsecutive(bool consecutive) {
	iniSettings->setValue("reject/isRejectConsecutive", consecutive);
}
bool IniManager::getRejectSignalization() {
	if (iniSettings->value("reject/isRejectSignalization").toString() == "false") {
		return false;
	}
	return true;
}
void IniManager::setRejectSignalization(bool signalization) {
	iniSettings->setValue("reject/isRejectSignalization", signalization);
}
bool IniManager::getRejectBelt() {
	if (iniSettings->value("reject/hasRejectBelt").toString() == "false") {
		return false;
	}
	return true;
}
void IniManager::setRejectBelt(bool hasBelt) {
	iniSettings->setValue("reject/hasRejectBelt", hasBelt);
}
int IniManager::getRejectBeltConOutPort() {
	return iniSettings->value("reject/rejectBeltPort").toInt();
}
void IniManager::setRejectBeltConOutPort(int port) {
	iniSettings->setValue("reject/rejectBeltPort", port);
}
int IniManager::getRejectTime() {
	return iniSettings->value("reject/rejectTime").toInt();;
}
void IniManager::setRejectTime(int time) {
	iniSettings->setValue("reject/rejectTime", time);
}
int IniManager::getRejectBackTime() {
	return iniSettings->value("reject/rejectBackTime").toInt();
}
void IniManager::setRejectBackTime(int backTime) {
	iniSettings->setValue("reject/rejectBackTime", backTime);
}

std::string IniManager::getPlcPort() {
	return iniSettings->value("plc/port").toString().toStdString();

}
void IniManager::setPlcPort(std::string port) {
	iniSettings->setValue("plc/port", port.c_str());
}

std::string IniManager::getIoPort() {
	return iniSettings->value("io/port").toString().toStdString();

}
void IniManager::setIoPort(std::string port) {
	iniSettings->setValue("io/port", port.c_str());

}

TubeConfig IniManager::getTubeConf() {
	return xrayTubeConf;
}

void IniManager::setTubeConf(TubeConfig conf) {
	xrayTubeConf = conf;
	setTubePort(conf.tube_com_no);
	setTubeType(conf.tube_name);
	iniSettings->sync();
}

ScannerConfig IniManager::getScannerConfig() {
	return xrayCameraConf;
}

void IniManager::setScannerConf(ScannerConfig conf) {
	xrayCameraConf = conf;
	setScannerType(conf.scanner_name);
	setScannerIp(conf.scanner_ip);
	setDaqIp(conf.scanner_daqip);
	setScannerControlPort(conf.scanner_control_port);
	setScannerImgPort(conf.scanner_image_port);
	setScannerIntegralTime(conf.scanner_int_time);
	iniSettings->sync();
}



BeltConfig IniManager::getBeltConfig() {
	return beltConf;
}

void IniManager::setBeltConf(BeltConfig conf) {
	beltConf = conf;
	setBeltPort(conf.belt_com_no);
	setBeltSpeed(conf.belt_speed);
	iniSettings->sync();
}

//PlcConfig IniManager::getPlcConfig() {
//	return plcConf;
//}

//void IniManager::setPlcConf(PlcConfig conf) {
//	plcConf = conf;
//	setPlcPort(conf.plc_com_no);
//	iniSettings->sync();
//}

SYSTEMConfig IniManager::getSysConf() {
	return sysConf;
}
void IniManager::setSysConf(SYSTEMConfig conf) {
	sysConf = conf;
	setSysLang(conf.language);
	setImgPath(conf.img_path);
	setLogPath(conf.log_path);
	setCollectionMode(conf.collectionMode);
	iniSettings->sync();
}

SQLConfig IniManager::getSQLConf() {
	return sqlConf;
}

void IniManager::setSQLConfig(SQLConfig conf) {
	sqlConf = conf;
	setDBType(conf.dbType); 
	setMySqlUserName(conf.userName);
	setMySqlPasswd(conf.passwd);
	setMySqlIp(conf.host);
	//setDBName(conf.dbName);
	//setDBCharSet(conf.charSet);
	setDBPort(conf.port); 
	iniSettings->sync();
}

bool IniManager::getDetectStripObj() {
	return iniSettings->value("algo/detectStripObj").toBool();
}
void IniManager::setDetectStripObj(bool val) {
	iniSettings->setValue("algo/detectStripObj", val);
}

std::string IniManager::getCalibrateUpdateTime() {
	return iniSettings->value("calibrate/update-time").toString().toStdString();
}
void IniManager::setCalibrateUpdateTime(std::string val) {
	iniSettings->setValue("calibrate/update-time", val.c_str());
}

bool IniManager::getForceCalibrate() {
	return forceCalibrate;
}
void IniManager::setForceCalibrate(bool val) {
	forceCalibrate = val;
}

bool IniManager::getPlcEnabledStatus() {

	if (iniSettings->value("plc/isPlcEnabled").toString() == "false") {
		isPlcEnabled = false;
	}
	return isPlcEnabled;
}
void IniManager::setPlcEnabledStatus(bool val) {
	isPlcEnabled = val;
}
float IniManager::getCalibrateVoltage() {
return iniSettings->value("calibrate/voltage").toFloat();
}
void IniManager::setCalibrateVoltage(float val) {
	iniSettings->setValue("calibrate/voltage", QString("%1").arg(val));
}

float IniManager::getCalibrateElectricity() {
	return iniSettings->value("calibrate/electricity").toFloat();
}
void IniManager::setCalibrateElectricity(float val) {
	iniSettings->setValue("calibrate/electricity", QString("%1").arg(val));
}

std::string IniManager::getBlowPort() {
	return iniSettings->value("blow/port").toString().toStdString();

}
void IniManager::setBlowPort(std::string port) {
	iniSettings->setValue("blow/port", port.c_str());

}